package com.atguigu.gulimail.product.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimail.product.dao.SkuInfoDao;
import com.atguigu.gulimail.product.entity.SkuInfoEntity;
import com.atguigu.gulimail.product.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        final IPage<SkuInfoEntity> page = new Query<SkuInfoEntity>().getPage(params);
        final QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();

        final String key = (String) params.get("key");
        if (!StringUtils.isBlank(key)) {
            wrapper.and(w -> w.eq("sku_id", key).or().like("sku_name", key));
        }
        String brandIdStr = (String) params.get("brandId");
        if (!StringUtils.isBlank(brandIdStr)) {
            long brandId = Long.parseLong(brandIdStr);
            if (brandId > 0) {
                wrapper.eq("brand_id", brandId);
            }
        }
        final String catelogIdStr = (String) params.get("catelogId");
        if (!StringUtils.isBlank(catelogIdStr)) {
            long catelogId = Long.parseLong(catelogIdStr);
            if (catelogId > 0) {
                wrapper.eq("catalog_id", catelogId);
            }
        }
        final String minStr = (String) params.get("min");
        if (!StringUtils.isBlank(minStr)) {
            final Integer min = Integer.parseInt(minStr);
            wrapper.ge("price", min);
        }
        final String maxStr = (String) params.get("max");
        if (!StringUtils.isBlank(maxStr)) {
            final int max = Integer.parseInt(maxStr);
            if (max > 0) {
                wrapper.le("price", max);
            }
        }
        IPage<SkuInfoEntity> pageX = this.page(
                page, wrapper);
        return new PageUtils(pageX);


    }

}